package cn.ylz.ylmall.product.controller;

import cn.ylz.ylmall.account.security.CurrentPrincipal;
import cn.ylz.ylmall.product.pojo.entity.Products;
import cn.ylz.ylmall.product.service.ProductsService;
import cn.ylz.ylmall.common.web.JsonResult;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/products")
@Api(tags = "3.1. 商品管理")
public class ProductsController {

    @Autowired
    private ProductsService productsService;
    
    
    /**
     * 添加/修改
     */
    @ApiOperation(value = "添加/修改商品")
    @PostMapping("/save")
    public JsonResult save(@RequestBody Products productsEntity) {
        productsService.save(productsEntity);
        return JsonResult.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除商品")
    public JsonResult delete(@PathVariable String id) {
        productsService.deleteById(id);
        return JsonResult.ok();
    }

    /**
     * 查询列表
     */
     @GetMapping("/list")
     @ApiOperation(value = "查询商品列表")
    public JsonResult list(Page page, String query) {
        IPage iPage = productsService.list(page, query);
        return JsonResult.ok(iPage);
}

    /**
     * 根据ID查询
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping("/get/{id}")
    public JsonResult getById(@PathVariable String id) {
        Products productsEntity = productsService.getById(id);
        return JsonResult.ok(productsEntity);
    }


    /**
     * 通过id集合删除
     */
    @DeleteMapping("/deletes")
    public JsonResult deleteByIds(@RequestBody List<String> ids) {
        productsService.deleteByIds(ids);
        return JsonResult.ok();
    }

    /**
     * 商品收藏
     */
    @ApiOperation(value = "(取消)收藏商品")
    @PostMapping("/like/{ProductsId}")
    public JsonResult collectProducts(@PathVariable Long ProductsId, @ApiIgnore @AuthenticationPrincipal CurrentPrincipal currentPrincipal) {
        return JsonResult.ok(productsService.collectProducts(ProductsId,currentPrincipal.getId()));
    }

    @ApiOperation(value = "判断某商品是否已经收藏")
    @PostMapping("/likeOrNot/{ProductsId}")
    public JsonResult likeOrNotProducts(@PathVariable Long ProductsId, @ApiIgnore @AuthenticationPrincipal CurrentPrincipal currentPrincipal){
        return JsonResult.ok(productsService.likeOrNotProducts(ProductsId,currentPrincipal.getId()));
    }


    @ApiOperation(value = "显示所有收藏的商品")
    @GetMapping("/likePage")
    public JsonResult showCollectProducts(Integer pageNum, @ApiIgnore @AuthenticationPrincipal CurrentPrincipal currentPrincipal){
        return JsonResult.ok(productsService.showCollectProducts(pageNum,currentPrincipal.getId()));
    }


}
